package db

import (
	log "github.com/sirupsen/logrus"
	"src/db/db_modle"
	"src/error_msg"
	"src/tools"
)

// 获取紧急联系人详细信息
func GetUrgencyContactsDetails(uid uint) []db_modle.UrgentLiaison {
	orm := GetMysqlOrm()
	defer orm.Close()
	var user []db_modle.UrgentLiaison
	result := orm.Where("user_id = ?", uid).Find(&user)
	if e := result.Error; e != nil {
		log.Error(e.Error())
		return []db_modle.UrgentLiaison{}
	}
	if result.RowsAffected == 0 {
		ne := error_msg.InformationNotFoundError
		log.Error(ne.Error())
		return user
	}
	return user
}

// 获取紧急联系人的记录id
// tips:数据库结构需要调整
func GetLastUrgencyContactsDetails(uid uint) (db_modle.UrgentLiaison, error) {
	orm := GetMysqlOrm()
	defer orm.Close()
	var user db_modle.UrgentLiaison
	result := orm.Where("user_id = ?", uid).Order("id desc").First(&user)
	if e := result.Error; e != nil {
		ne := error_msg.InformationNotFoundError
		log.Error(ne.Error())
		return db_modle.UrgentLiaison{}, ne
	}
	if result.RowsAffected == 0 {
		ne := error_msg.InformationNotFoundError
		log.Error(ne.Error())
		return user, ne
	}
	return user, nil
}

// 获取紧急联系人电话集合
func GetUCInfo(uid uint) (map[string]int, error) {
	bd, e := GetLastUrgencyContactsDetails(uid)
	if e != nil {
		return map[string]int{}, e
	}
	orm := GetMysqlOrm()
	defer orm.Close()

	pList, e := GetLiaisonInfo(bd.Id)
	if e != nil {
		log.Error(e)
		return map[string]int{}, e
	}
	// 拿到去重后的紧急联系人
	return tools.RemoveDuplicate(pList), nil
}

// 紧急联系人贷款逾期天数
func GetDaysOfLastUrgencyContactsOverdue(uid uint, logId string) (int, error) {
	bd, e := GetLastUrgencyContactsDetails(uid)
	if e != nil {
		return 0, e
	}
	orm := GetMysqlOrm()
	defer orm.Close()
	var max int64
	// RemoveDuplicateContacts
	RDContacts := make(map[string]int)

	pList, e := GetLiaisonInfo(bd.Id)
	if e != nil {
		log.Error(e.Error(), logId)
		return 0, e
	}

	// 拿到去重后的紧急联系人
	RDContacts = tools.RemoveDuplicate(pList)

	// 遍历紧急联系人
	for k := range RDContacts {
		user := db_modle.Users{}
		// 通讯录中各个联系人电话
		res := orm.Where("phone = ?", k).First(&user)
		if e := res.Error; e != nil {
			// 未注册的用户没有电话，查不到跳过
			log.Info("用户的联系人未查找到对应数据", logId)
			continue
		}
		// pay表中uid和review_type,获取借到钱的人订单id
		var orderIds []db_modle.Pay
		// review_type=1 已贷款
		r := orm.Where("user_id = ?", user.Id).Where("review_type = ?", 1).Find(&orderIds)
		if e := r.Error; e != nil {
			log.Error(e.Error(), logId)
			return 0, e
		}
		// repayment中订单id获取对应的also_time, 与越南当地时间比对
		repayment := db_modle.Repayment{}
		for _, v := range orderIds {
			s := orm.Where("order_id = ?", v.OrderId).Where("repayment_status != ?", 3).First(&repayment)
			if e := s.Error; e != nil {
				// 未查询到，不能影响其他订单的查询
				continue
			}
			// 逾期
			if tools.GetVietnamTime().Unix()-repayment.AlsoTime.Unix() > max {
				max = tools.GetVietnamTime().Unix() - repayment.AlsoTime.Unix()
				log.Debugf("用户紧急联系人贷款逾期 %+v \n", int(max/(60*60*24)))
			}
		}
	}
	return int(max / (60 * 60 * 24)), nil
}

// 紧急联系人号码使用次数
func GetNumOfLastUrgencyContacts(uid uint, logId string) (int64, error) {
	bd, e := GetLastUrgencyContactsDetails(uid)
	if e != nil {
		log.Error(e.Error(), logId)
		return 0, e
	}
	orm := GetMysqlOrm()
	defer orm.Close()
	var max int64
	// RemoveDuplicateContacts
	RDContacts := make(map[string]int)

	// liaison表 获取联系人相情
	pList, e := GetLiaisonInfo(bd.Id)
	if e != nil {
		log.Error(e.Error(), logId)
		return 0, e
	}

	// 拿到去重后的紧急联系人
	RDContacts = tools.RemoveDuplicate(pList)

	// 遍历紧急联系人
	for k := range RDContacts {
		count, _ := GetNumOfUrgentContacts(k)
		if count > max {
			max = count
		}
	}
	return max, nil
}

// 近7天与紧急联系人联系次数
